//
//  BaseViewController.m
//  Account
//
//  Created by jty on 2020/6/22.
//

#import "BaseViewController.h"
#import "UIImage+Color.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.view.backgroundColor = HEXCOLOR(0xF5F5F5);
    self.navigationItem.title = @"借条云";
    self.view.backgroundColor = UIColor.whiteColor;
    
    if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance *appperance = [[UINavigationBarAppearance alloc]init];
        //添加背景色
        appperance.backgroundColor = HEXCOLOR(0x333333);
        appperance.shadowImage = [[UIImage alloc]init];
        appperance.shadowColor = nil;
        //设置字体颜色大小
        [appperance setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:17]}];
        self.navigationController.navigationBar.standardAppearance = appperance;
        self.navigationController.navigationBar.scrollEdgeAppearance = appperance;
    }
    UINavigationBar *navigationBar = self.navigationController.navigationBar;
    [navigationBar setBackgroundImage:[UIImage imageWithColor:HEXCOLOR(0x333333)]
                       forBarPosition:UIBarPositionAny
                           barMetrics:UIBarMetricsDefault];
    [navigationBar setShadowImage:[UIImage new]];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor],
    NSFontAttributeName : [UIFont fontWithName:@"Helvetica-Bold" size:17]}];
    
    // 隐藏导航栏的返回按钮
    [self.navigationItem setHidesBackButton:YES animated:NO];
}
- (void)setHasLeftBar:(BOOL)hasLeftBar{
    if (hasLeftBar){
        [self setNavigate]; //>父级导航栏
    }else{
        self.navigationItem.leftBarButtonItem = nil;
    }
}
-(void)setNavigate{
//    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
//    UIImage *image = [UIImage imageNamed:@"baseBack"];
////    if (image) {
////        image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
////    }
//    self.navigationController.navigationBar.backIndicatorImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    self.navigationController.navigationBar.backIndicatorTransitionMaskImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    self.navigationItem.backBarButtonItem = backItem;
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 30, 30);
    [backButton setImage:[UIImage imageNamed:@"baseBack"] forState:UIControlStateNormal];

    backButton.imageEdgeInsets = UIEdgeInsetsMake(0, -18, 0, 0);//
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

    self.navigationItem.leftBarButtonItem = backItem;
}

/**
 *  统一返回事件
 */
-(void)back{
    if (self.navigationController.viewControllers.count>1) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
#pragma mark 堆窄部分
// 快速push
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    // 如果有大于控制器
    viewController.hidesBottomBarWhenPushed = YES;
    dispatch_async(dispatch_get_main_queue(), ^{
      [self.navigationController pushViewController:viewController animated:animated];
    });
}

// 快捷返回
- (void)popToRoot:(BOOL)root Animated:(BOOL)animated
{
    if (root)
    {
        [self.navigationController popToRootViewControllerAnimated:animated];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:animated];
    }
}

// 从navc中移除自己
- (void)removeSelfFromNavigation
{
    NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers];
    [navigationArray removeObject:self];
    self.navigationController.viewControllers = navigationArray;
}
/**
 *销毁事件
 
 */
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    NSLog(@"%@  dealloc...",NSStringFromClass([self class]));
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
